/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-02 11:57:13
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 10:40:59
 */
// components/commentCard/index.js

import {
  utils
} from './../../components/lib/utils'

import {
  returnStatusToast
} from '../../utils/util'
import {
  noteAPI
} from '../../api/note'

import Toast from '../../@vant/weapp/dist/toast/toast';
import Notify from '../..//@vant/weapp/dist/notify/notify';
Component({
  externalClasses: ['custom-class', 'content-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    size: {
      type: String,
      value: 'sm'
    },
    baseline:{
      type:Boolean,
      value: true
    },
    expand: Boolean,
    reply: Boolean,
    nick: String,
    likeCount: Number,
    isMaster: Boolean,
    isDel: Boolean,
    data: {
      type: Object,
      value: {}
    }
  },
  ready() {
    this.setData({
      createDate: utils.relativeTime(this.data.data.createDate),
      likes: this.data.data.likeCount,
      alreadyLike: this.data.data.alreadyLike === 'YES'
    })
  },
  /**
   * 组件的初始数据
   */
  data: {
    createDate: '',
    likes: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toInfo(){
      wx.navigateTo({
        url: '/pages/mine/home/index?customerId='+this.data.data.authorId
      });
    },
    async delReply() {
      try {
        const _ = await noteAPI.dropReply({
          replyId: this.data.data.replyId
        })
        returnStatusToast(_, () => {
          this.triggerEvent('refreshReply')
        })
      } catch (_) {
        Notify({
          type: 'warning',
          message: _,
          duration: 800
        });
      }
    },
    replayCountAction() {
      this.triggerEvent('subReply', {
        replyToId: this.data.data.authorId,
        replyId: this.data.data.replyId
      })
    },
    async handleLike() {
      const _ = await noteAPI.handleLikeReply({
        replyId: this.data.data.replyId,
        type: this.data.alreadyLike ? 'NO' : 'YES'
      })
      returnStatusToast(_, _ => {
        this.setData({
          alreadyLike: !this.data.alreadyLike,
          likes: !this.data.alreadyLike ? ++this.data.likes : --this.data.likes
        })
      })
    }
  }
})